package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.gulimall.ware.entity.WareInfoEntity;
import com.atguigu.gulimall.ware.service.WareInfoService;
import com.atguigu.gulimall.ware.vo.SkuHasStockVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.WareSkuDao;
import com.atguigu.gulimall.ware.entity.WareSkuEntity;
import com.atguigu.gulimall.ware.service.WareSkuService;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private WareSkuDao wareSkuDao;

    @Autowired
    private WareInfoService wareInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isEmpty(skuId)) {
            wrapper.eq("sku_id", skuId);
        }
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)) {
            wrapper.eq("ware_id", wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );
        //将参数进行处理
        List<WareSkuEntity> records = page.getRecords();
        records = records.stream().map( item ->{
            Long id = item.getWareId();
            if (!ObjectUtils.isEmpty(id)) {
                WareInfoEntity wareInfoEntity = wareInfoService.getById(id);
                if (!ObjectUtils.isEmpty(wareInfoEntity)){
                    item.setWareName(wareInfoEntity.getName());
                }
            }
            return item;
        }).collect(Collectors.toList());
        return new PageUtils(page);
    }

    @Override
    public void addStock(WareSkuEntity skuEntity) {
        //首先根据仓库id和skuid进行查看是否以及存在该商品
        Integer num = wareSkuDao.selectCount(skuEntity.getSkuId(), skuEntity.getWareId());
        //说明已经存在该商品了，我们就对其数量进行修改
        if (num > 0) {
            wareSkuDao.addStock(skuEntity.getSkuId(), skuEntity.getWareId(), skuEntity.getStock());
        }else {
            this.save(skuEntity);
        }
    }

    @Override
    public List<SkuHasStockVO> getSkuHasStock(List<Long> skuIds) {
        List<SkuHasStockVO> collect = skuIds.stream().map(skuId -> {
            SkuHasStockVO vo = new SkuHasStockVO();
            Long count = baseMapper.getSkuStock(skuId);
            vo.setSkuId(skuId);
            vo.setHasStock( count == null ?false:count>0);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

}